package com.mall.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mall.business.domain.TCoupon;
import com.mall.business.service.ITCouponService;
import com.mall.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2025-03-29
 */
@RestController
@RequestMapping("/business/coupon")
public class TCouponController extends BaseController {
    @Autowired
    private ITCouponService tCouponService;

    /**
     * 查询优惠券列表
     */
//@RequiresPermissions("business:coupon:list")
    @GetMapping("/list")
    public TableDataInfo list(TCoupon tCoupon) {
        startPage();
        List<TCoupon> list = tCouponService.selectTCouponList(tCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    //@RequiresPermissions("business:coupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCoupon tCoupon) {
        List<TCoupon> list = tCouponService.selectTCouponList(tCoupon);
        ExcelUtil<TCoupon> util = new ExcelUtil<TCoupon>(TCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    //@RequiresPermissions("business:coupon:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tCouponService.getById(id));
    }

    /**
     * 新增优惠券
     */
    //@RequiresPermissions("business:coupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCoupon tCoupon) {
        return toAjax(tCouponService.save(tCoupon));
    }

    /**
     * 修改优惠券
     */
    //@RequiresPermissions("business:coupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCoupon tCoupon) {
        return toAjax(tCouponService.updateById(tCoupon));
    }

    /**
     * 删除优惠券
     */
    //@RequiresPermissions("business:coupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tCouponService.removeByIds(Arrays.asList(ids)));
    }
}
